package com.ktjiaoyu.controller;


import com.ktjiaoyu.entity.*;
import com.ktjiaoyu.service.CommentsService;
import com.ktjiaoyu.utils.Constants;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/main/comments/")
public class CommentsController extends BaseController{
    private static final Logger LOGGER= LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    CommentsService commentsService;

    @RequestMapping("/page")
    public String page(Model model,
                       Comments condition,
                       @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        Page<Comments> page=null;
        try{
            page=commentsService.getPage(condition,pageNo,pageSize);

            model.addAttribute("pageInfo",page);
            model.addAttribute("condition",condition);
        }catch (Exception e){
            LOGGER.error("SysUserController中的page方法执行错误.",e);
            throw new RuntimeException(e);
        }
        return "comments/page";
    }

    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Long id,Model model){
        Comments comments=null;
        try {
            comments=new Comments();
            comments.setId(id);
            model.addAttribute("comments",comments);
        }catch (Exception e){
            LOGGER.error("SysUserController中的toEdit方法执行发生错误",e);
            throw new RuntimeException(e);
        }
        return "comments/save";
    }



    @PostMapping("/save")
    public String doSave(Comments comments){
        try{
            boolean ok=commentsService.updateReply(comments);
            if(ok){
                return redirect("/main/comments/page");
            }
        }catch (Exception e){
            LOGGER.error("SysRoleController中的doSave方法执行错误",e);
        }
        return "comments/save";
    }


    @DeleteMapping("/del/{id}")
    @ResponseBody
    public String del(@PathVariable("id") Long id){
        try{
            int result=commentsService.deleteById(id);
            return  ajaxResult();
        }catch (Exception e){
            LOGGER.error("del方法执行错误");
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/del/{id}/{status}")
    @ResponseBody
    public String updateStatus(@PathVariable("id") Long id,@PathVariable("status") Long status){
        try{
            int result=commentsService.updateStatus(id,status);
            return  ajaxResult();
        }catch (Exception e){
            LOGGER.error("del方法执行错误");
            throw new RuntimeException(e);
        }
    }


//    @PostMapping("/updateIsDel")
//    public String doSave(AttrValues attrValues){
//        try{
//            Long orderNum=attrValueService.count()+1;
//            attrValues.setOrderNum(orderNum);
//            boolean ok=attrValueService.saveOrUpdate(attrValues);
//            if(ok){
//                return redirect("/main/attrValue/page");
//            }
//        }catch (Exception e){
//            LOGGER.error("SysUserController中的doSave方法执行错误",e);
//        }
//        return "attrValue/save";
//    }
}
